package com.daxt.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.archives.ArchivesDocFileMapper;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.mapper.archives.ArticleMapper;
//import com.daxt.mapper.archives.ElasticRepository;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.model.service.archives.vo.ArchivesDocFile;
import com.daxt.model.service.archives.vo.Article;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
//import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Api(tags = "ec调试")
@RestController
@RequestMapping("/elastic")
public class ElasticController {


//    @Autowired
//    private IElasticService elasticService;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArchivesDocMapper archivesDocMapper;
    @Autowired
    private ArchivesDocFileMapper archivesDocFileMapper;
//    @Autowired
//    private ElasticRepository elasticRepository;

    @GetMapping("/init")
    public void init() {


        QueryWrapper<ArchivesDoc> qw = new QueryWrapper();
        qw.eq("state","2");
        List<ArchivesDoc> archivesDocList = archivesDocMapper.selectList(qw);
        archivesDocList.forEach((ArchivesDoc doc)->{

            Article a = new Article();
            a.setId(doc.getId());
            a.setTitle(doc.getTitle());

            //附件
            QueryWrapper<ArchivesDocFile> qwFile = new QueryWrapper<ArchivesDocFile>();
            qwFile.eq("docId",doc.getId());
            List<ArchivesDocFile> archivesDocFileList = archivesDocFileMapper.selectList(qwFile);
            for(ArchivesDocFile adf : archivesDocFileList){
                if(StringUtils.isEmpty(a.getFileName())){
                    a.setFileName(adf.getFileName());
                }else{
                    a.setFileName(a.getFileName() + "," + adf.getFileName());
                }
            }

            articleMapper.insert(a);

//            DocBean db = new DocBean();
//            db.setId(doc.getId());
//            db.setTitle(doc.getTitle());
//            elasticRepository.save(db);
        });

//        List<ArchivesDocFile> archivesDocFileList = archivesDocFileMapper.selectList(new QueryWrapper<ArchivesDocFile>());
//        archivesDocFileList.forEach((ArchivesDocFile adf)->{
//
//            Article b = articleMapper.selectById(adf.getDocId());
//            if(null == b ){
//                b = new Article();
//                b.setId(adf.getDocId());
//                b.setFileName(adf.getFileName());
//                articleMapper.insert(b);
//            }else{
//                b.setFileName(b.getFileName() + "," + adf.getFileName());
//                articleMapper.updateById(b);
//            }



//            Optional<DocBean> oDb = elasticRepository.findById(adf.getDocId());
//            DocBean db2 =  oDb.get();
//            if(null != db2){
//                db2.setFileName(adf.getFileName());
//            }else{
//                db2 = new DocBean();
//                db2.setId(adf.getId());
//                db2.setFileName(adf.getFileName());
//            }
//            elasticRepository.save(db2);
//        });

//
////        elasticService.createIndex();
//        List<DocBean> list = new ArrayList<>();
////
////        ArchivesDoc ad1 = new ArchivesDoc();
////        ad1.setTitle("路桥一公司");
////
////        ArchivesDoc ad2 = new ArchivesDoc();
////        ad2.setTitle("路桥二公司");
////
////        ArchivesDoc ad3 = new ArchivesDoc();
////        ad3.setTitle("路桥三公司");
////
////
////        list.add(ad1);
////        list.add(ad2);
////        list.add(ad3);
//
//        list.add(new DocBean(3L, "XX0193", "XX8064", "以热爱祖国为荣、以危害祖国为耻", 1));
//        list.add(new DocBean(4L, "XX0210", "XX7475", "以服务人民为荣、以背离人民为耻", 1));
//        list.add(new DocBean(5L, "XX0257", "XX8097", "以崇尚科学为荣，以愚昧无知为耻", 1));
//        elasticService.saveAll(list);

    }

//    @GetMapping("/all")
//    public Iterable<DocBean> all() {
//        return (Iterable<DocBean>) elasticRepository.findAll();
//    }
//
//    @GetMapping("/queryKey")
//    public Result<List<DocBean>> queryKey(String key) {
//
////        RestHighLevelClient client = new RestHighLevelClient(
////                RestClient.builder(
////                        //在这里配置你的elasticsearch的情况
////                        new HttpHost("127.0.0.1", 9200, "http")
////                )
////        );
//
//
//
//        Pageable pageable = PageRequest.of(0,10);
////        Page<DocBean> docBean = elasticRepository.findByTitleOrFileNameOrText(key,key,key,pageable);
////        Page<DocBean> docBean = elasticRepository.findByText(key,pageable);
//        Page<DocBean> docBean = elasticRepository.findByTile(key,pageable);
////        Optional<DocBean> d = elasticRepository.findById(Long.parseLong(id));
//
////        Page docBean = elasticRepository.findByContent(code,pageable);
//
//
//
////        Optional<DocBean> d = elasticRepository.findById(1l);
//
//        return ResultUtil.success(docBean.getContent());
//    }

}
